package org.skiplist2;

import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

public class performanceTest {

    public Random random = new Random();
    public final int size = 100000;
    public final int time = 10;

    @Test
    public void SkipListTest() {
        long[] res = new long[time];
        long begin, end;
        for (int i = 0; i < time; i++) {
            int[] nums = getRandomArray(size);
            SkipList<Integer> skipList = new SkipList<>();
            begin = System.currentTimeMillis();
            for (Integer num : nums) {
                skipList.add(num);
            }
            end = System.currentTimeMillis();
            res[i] = end - begin;
        }
        Arrays.sort(res);
        long sum = 0;
        for (long n : res) {
            sum += n;
        }
        double average = sum / 10.0;
        System.out.println("max: " + res[res.length -1]);
        System.out.println("min: " + res[0]);
        System.out.println("average: " + average);
        System.out.println(Arrays.toString(res));
    }

    @Test
    public void TreeSetTest() {
        long[] res = new long[time];
        long begin, end;
        for (int i = 0; i < time; i++) {
            int[] nums = getRandomArray(size);
            TreeSet<Integer> treeSet = new TreeSet<>();
            begin = System.currentTimeMillis();
            for (Integer num : nums) {
                treeSet.add(num);
            }
            end = System.currentTimeMillis();
            res[i] = end - begin;
        }
        Arrays.sort(res);
        long sum = 0;
        for (long n : res) {
            sum += n;
        }
        double average = sum / 10.0;
        System.out.println("max: " + res[res.length -1]);
        System.out.println("min: " + res[0]);
        System.out.println("average: " + average);
        System.out.println(Arrays.toString(res));
    }

    public int[] getRandomArray(int size) {
        int[] nums = new int[size];
        for (int i = 0; i < size; i++) {
            nums[i] = random.nextInt();
        }
        return nums;
    }

    public List<Integer> getRandomList(int size) {
        List<Integer> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            list.add(random.nextInt());
        }
        return list;
    }
}

